const mongoose = require('mongoose')

mongoose.connect('mongodb://localhost:27017/express-auth', {
  useCreateIndex: true,
  useNewUrlParser: true
})

const User = mongoose.model('User', new mongoose.Schema({
  username: { type: String, unique: true },
  password: { type: String, 
    set(val){
      // 这里加传到模型的密码进行加密存入数据库
      return require('bcrypt').hashSync(val, 10)// 散列加密，第二个参数越大安全性越高，但相应越耗时间
    } 
  }
}))

module.exports = {
  User
}